package com.dyna.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyna.domain.req.PageParam;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import com.dyna.enums.CommonStatusEnum;
import com.dyna.excel.core.util.ExcelUtil;
import com.dyna.foundation.domain.entity.SysDictData;
import com.dyna.foundation.domain.req.SysDictDataPageReq;
import com.dyna.foundation.domain.resp.SysDictDataResp;
import com.dyna.foundation.domain.resp.SysDictDataSaveForm;
import com.dyna.foundation.domain.resp.SysDictDataSimpleResp;
import com.dyna.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@Api(value = "字典数据")
@RestController
@RequestMapping("/dict-data")
@Validated
public class SysDictDataController {

    @Resource
    private SysDictDataService dictDataService;

    @PostMapping("/create")
    @ApiOperation(value = "新增字典数据")
    public R<Integer> createDictData(@Valid @RequestBody SysDictDataSaveForm createReqVO) {
        Integer dictDataId = dictDataService.createDictData(createReqVO);
        return R.data(dictDataId);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改字典数据")
    public R<Boolean> updateDictData(@Valid @RequestBody SysDictDataSaveForm updateReqVO) {
        dictDataService.updateDictData(updateReqVO);
        return R.data(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除字典数据")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<Boolean> deleteDictData(Long id) {
        dictDataService.deleteDictData(id);
        return R.data(true);
    }

    @GetMapping(value = {"/list-all-simple", "simple-list"})
    @ApiOperation(value = "获得全部字典数据列表", notes = "一般用于管理后台缓存字典数据在本地")
    // 无需添加权限认证，因为前端全局都需要
    public R<List<SysDictDataSimpleResp>> getSimpleDictDataList() {
        List<SysDictDataSimpleResp> list = dictDataService.getDictDataList(
                CommonStatusEnum.ENABLE.getStatus(), null);
        return R.data(list);
    }

    @GetMapping("/page")
    @ApiOperation(value = "获得字典类型的分页列表")
    public R<PageResult<SysDictDataResp>> getDictTypePage(@Valid SysDictDataPageReq pageReqVO) {
        PageResult<SysDictDataResp> pageResult = dictDataService.getDictDataPage(pageReqVO);
        return R.data(pageResult);
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "/查询字典数据详细")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024")})
    public R<SysDictDataResp> getDictData(@RequestParam("id") Long id) {
        SysDictData dictData = dictDataService.getDictData(id);
        return R.data(BeanUtil.toBean(dictData, SysDictDataResp.class));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出字典数据")
    public void export(HttpServletResponse response, @Valid SysDictDataPageReq exportReqVO) throws IOException {
        exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysDictDataResp> list = dictDataService.getDictDataPage(exportReqVO).getList();
        // 输出
        ExcelUtil.write(response, "字典数据.xls", "数据", SysDictDataResp.class,list);
    }

}
